import path from 'path'

/**
 * pinia 适配插件
 * @param {import('@tarojs/service').IPluginContext} ctx 上下文对象
 */
export default ctx => {
  ctx.modifyWebpackChain(({ chain }) => {
    const { runOpts, helper } = ctx
    const { options } = runOpts

    if (options.platform !== helper.PLATFORMS.H5) {
      chain.plugin('providerPlugin').tap(args => {
        return [
          {
            ...(args[0] || {}),
            HTMLAnchorElement: [
              path.resolve(__dirname, './constructor.js'),
              'HTMLAnchorElement'
            ]
          }
        ]
      })
    }

    chain.module
      .rule('mjs')
      .test(/\.mjs$/)
      .type('javascript/auto')
      .include.add(/node_modules/)
      .end()
  })
}
